﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Sistema
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
            txtsenha.PasswordChar = '*';
        }

 
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                
                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = new MySqlCommand("select * from mydb.*;", Conexao.Conn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(da);

                Conexao.Conn.Open();
                //DataSet ds = new DataSet();
                MessageBox.Show("Conectado com sucesso");
                Conexao.Conn.Close();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Não foi Possivel se conectar ao Banco de Dados");
                Console.WriteLine(ex);
            }

        }

        private void btlogar_Click(object sender, EventArgs e)
        {
            try {


                
                MySqlCommand SelectCommand = new MySqlCommand("select * from mydb.usuario where user='"+this.txtuser.Text+"' and senha='"+this.txtsenha.Text+"';", Conexao.Conn);
                MySqlDataReader result;
                Conexao.Conn.Open();
                result = SelectCommand.ExecuteReader();

                int count = 0;
                while(result.Read())
                {
                    count++;
                }
                if (count == 1)
                {
                    Conexao.Conn.Close();
                    this.Hide();
                    result.Close();
                   new FrmPrincipal().ShowDialog();
                 }
       
                else
                    MessageBox.Show("Usuario e/ou senha incorretos. Por favor tente novamente");
                Conexao.Conn.Close();
             
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);  
            }
        }

        private void criarUsuárioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmCadLogin novo = new FrmCadLogin();
            novo.ShowDialog();
        }


        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
